<?php
/**
 * huhaiqin,2025.2.5
 * 排行榜接口
 */
namespace Game\Controller;
use Game\Controller\GameBaseController;

class RankController extends GameBaseController{

    public function get_last_rank_info(){
        // 获取当前时间
        phpinfo();
        var_dump(extension_loaded('redis')); // 应返回bool(true)
        var_dump(class_exists('Redis'));     // 应返回bool(true)
        $current_time = date('Y-m-d H:i:s');
        S('rank_info', $current_time, 10);
        // S('rank_info', $current_time, ['expire' => 10]);
        dump(S('rank_info'));
        // dump('111');
        // $time_stamp = strtotime('this week 00:00:00');
        // dump(date('Y-m-d H:i:s', $time_stamp));
        // $time_stamp = strtotime('next week 00:00:00');
        // dump(date('Y-m-d H:i:s', $time_stamp));
        // $time_stamp = strtotime('first day of next month 00:00:00');
        // dump(date('Y-m-d H:i:s', $time_stamp));
        // dump(_get_reward_by_rank($this->_uid));
    }
    /**
     * 获取排行榜信息
     * idol_id 羁绊ID
     */
    public function get_rank_info(){
        $show_rank_limit = 20;
        $idol_id = I('idol_id');
        if(!$idol_id){
            _apiReturn(0, '羁绊ID不能为空');
            return;
        }
        $mode = I('mode', 'week');
        // 从缓存中读当前排名信息
        $res = [
            // 羁绊间的相互排名
            "idol_rank_list" => _get_idol_support_rank($mode),
            // 羁绊下的玩家排名，前1000
            "user_rank_list" => _get_user_support_rank($idol_id, $mode)
        ];
        // 查询用户当前个人排名
        $user_info = D('GameSupportRank')->get_user_support_info($this->_uid, $idol_id, $mode);
        // 有排行榜的support记录，则进行排名比较，否则按0处理，也不用排名了
        if($user_info){
            // 积分比第1000名的还要低，那就不比较了
            if($user_info['support'] < end($res['user_rank_list'])['support'] || ($user_info['support'] == end($res['user_rank_list'])['support'] && $user_info['update_time'] > end($res['user_rank_list'])['update_time'])){
                $user_info['rank'] = 1000;
                // 取前100项
            }else{
                foreach($res['user_rank_list'] as $k => $v){
                    if($user_info['support'] >= $v['support'] || ($user_info['support'] == $v['support'] && $user_info['update_time'] <= $v['update_time'])){
                        $user_info['rank'] = $k + 1;
                        break;
                    }
                }
            }
        }else{
            $user_info = [
                'support' => 0,
                'rank' => 1000
            ];
        }
        $res['user_rank_list'] = array_slice($res['user_rank_list'], 0, $show_rank_limit);
        $res['user_rank_info'] = $user_info;
        _apiReturn(1, '获取成功', $res);
    }

    /**
     * 领取排行奖励
     * type 支持support，贡献contribute
     * mode 周week，月month，年year
     */
    public function claim_rank_reward(){
        $type = I('type', 'support');
        $mode = I('mode', 'week');
        $res = D('GameUser', 'Service')->claim_rank_reward($this->_uid, $type, $mode);
        if(!$res['status']){
            _apiReturn(0, $res['msg']);
            return;
        }
        _write_user_log($this->_uid, 'claim_rank_reward', '', $res['data']['reward_item_list']);
        _apiReturn(1, '领取成功', ['reward_item_list' => $res['data']['reward_item_list'], 'user_info' => $this->_get_full_user_info()]);

    }
}